package com.hsteach.web.controller.patrol;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Maps;
import com.hsteach.common.annotation.Anonymous;
import com.hsteach.common.core.controller.BaseController;
import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.core.page.TableDataInfo;
import com.hsteach.common.exception.ServiceException;
import com.hsteach.common.utils.StringUtils;
import com.hsteach.common.utils.poi.ExcelUtil;
import com.hsteach.enums.DeleteStatusEnum;
import com.hsteach.enums.SourceEnum;
import com.hsteach.enums.StatusEnum;
import com.hsteach.framework.feign.client.upload.vo.UploadRet;
import com.hsteach.framework.web.service.UploadService;
import com.hsteach.service.patrol.InspectionService;
import com.hsteach.web.controller.patrol.dto.InspectionDetailDTO;
import com.hsteach.web.controller.patrol.dto.InspectionExportDTO;
import com.hsteach.web.controller.patrol.dto.QueryInspectionDTO;
import com.hsteach.web.controller.patrol.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author mgb
 * @version InspectionController, java v1.0 2023-11-22 1:01 EXP $
 */
@Api(value = "日常巡检", tags = "日常巡检")
@RestController
@RequestMapping("/patrol/inspection")
public class InspectionController extends BaseController {

    @Autowired
    public InspectionService inspectionService;
    @Autowired
    private UploadService uploadService;


    @ApiOperation("巡检班级打分")
    @PostMapping("/addInspection")
    public AjaxResult addInspection(@RequestBody InspectionDetailDTO inspectionDetailDTO) {
        if (StringUtils.isEmpty(inspectionDetailDTO.getOrgCode())) {
            throw new ServiceException("orgCode不能为空");
        }
        inspectionDetailDTO.setIsDelete(DeleteStatusEnum.UN_DELETE.getCode());
        inspectionDetailDTO.setStatus(StatusEnum.UN_CANCEL.getCode());
        return toAjax(inspectionService.addInspection(inspectionDetailDTO));
    }


    @ApiOperation("巡检班级打分撤销")
    @PostMapping("/deleteInspection")
    public AjaxResult deleteInspection(@RequestBody InspectionDetailDTO inspectionDetailDTO) {
        inspectionDetailDTO.setIsDelete(DeleteStatusEnum.UN_DELETE.getCode());
        inspectionDetailDTO.setStatus(StatusEnum.CANCEL.getCode());
        return success("撤销成功", inspectionService.deleteInspection(inspectionDetailDTO));
    }


    @ApiOperation("分页查询班级巡检得分")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody QueryInspectionDTO queryInspectionDTO) {
        if (StringUtils.isEmpty(queryInspectionDTO.getOrgCode())) {
            throw new ServiceException("orgCode不能为空");
        }
        return inspectionService.list(queryInspectionDTO);

    }

    @ApiOperation("班级巡查得分")
    @PostMapping("/queryInspectionScoreByClass")
    public TableDataInfo queryInspectionScoreByClass(@RequestBody QueryInspectionDTO queryInspectionDTO) {
        if (StringUtils.isEmpty(queryInspectionDTO.getOrgCode())) {
            throw new ServiceException("orgCode不能为空");
        }
        return inspectionService.queryInspectionByClass(queryInspectionDTO);
    }


    @ApiOperation("班级导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody InspectionExportDTO inspectionExportDTO) throws IOException {
        if (StringUtils.isEmpty(inspectionExportDTO.getOrgCode())) {
            throw new ServiceException("orgCode不能为空");
        }
        ExportVo exportVo = inspectionService.exportClassSore(inspectionExportDTO);
        if (SourceEnum.APP.getCode() == inspectionExportDTO.getSource()) {
            ExcelUtil<InspectionExportVO> util = new ExcelUtil<InspectionExportVO>(InspectionExportVO.class);
            String title = inspectionExportDTO.getGrade() + "," + inspectionExportDTO.getClasses() + ",减分" + exportVo.getScoreVO().getReduceTotalScore() + "分，加分" +
                    exportVo.getScoreVO().getAddTotalScore() + "分，总得分" + exportVo.getScoreVO().getTotalScore() + "分";
            AjaxResult success = AjaxResult.success();
            Map<String, String> data = Maps.newHashMap();
            Workbook wb = util.exportWbWithTitle(exportVo.getDataList(), inspectionExportDTO.getTitleName(), title);
            UploadRet upload = uploadService.upload(wb, inspectionExportDTO.getTitleName() + ".xlsx");
            data.put("url", upload.getUrl());
            success.put("data", data);
            response.getWriter().write(JSON.toJSONString(success));

        } else {
            ExcelUtil<InspectionExportVO> util = new ExcelUtil<InspectionExportVO>(InspectionExportVO.class);
            util.exportExcel(response, exportVo.getDataList(), inspectionExportDTO.getTitleName());

        }

    }

    @ApiOperation("班级评比得分导出")
    @PostMapping("/export/score")
    public void exportScore(HttpServletResponse response, @RequestBody InspectionExportDTO inspectionExportDTO) throws IOException {
        if (StringUtils.isEmpty(inspectionExportDTO.getOrgCode())) {
            throw new ServiceException("orgCode不能为空");
        }
        ExportVo2 exportVo = inspectionService.exportClassSore2(inspectionExportDTO);
        if (SourceEnum.APP.getCode() == inspectionExportDTO.getSource()) {
            ExcelUtil<InspectionExportVo2> util = new ExcelUtil<InspectionExportVo2>(InspectionExportVo2.class);
            String title = inspectionExportDTO.getGrade() + "," + inspectionExportDTO.getClasses() + ",减分" + exportVo.getScoreVO().getReduceTotalScore() + "分，加分" +
                    exportVo.getScoreVO().getAddTotalScore() + "分，总得分" + exportVo.getScoreVO().getTotalScore() + "分";
            AjaxResult success = AjaxResult.success();
            Map<String, String> data = Maps.newHashMap();
            Workbook wb = util.exportWbWithTitle(exportVo.getDataList(), inspectionExportDTO.getTitleName(), title);
            UploadRet upload = uploadService.upload(wb, inspectionExportDTO.getTitleName() + ".xlsx");
            data.put("url", upload.getUrl());
            success.put("data", data);
            response.getWriter().write(JSON.toJSONString(success));
        } else {
            ExcelUtil<InspectionExportVo2> util = new ExcelUtil<InspectionExportVo2>(InspectionExportVo2.class);
            util.exportExcel(response, exportVo.getDataList(), inspectionExportDTO.getTitleName());
        }
    }

    @ApiOperation("班级巡查得分明细")
    @PostMapping("/queryInspectionScoreDetail")
    public AjaxResult queryInspectionScoreDetail(@RequestBody QueryInspectionDTO queryInspectionDTO) {
        if (StringUtils.isEmpty(queryInspectionDTO.getOrgCode())) {
            throw new ServiceException("orgCode不能为空");
        }
        InspectionScoreDetailVO scoreDetailVO = inspectionService.queryInspectionScoreDetail(queryInspectionDTO);
        return success(scoreDetailVO);
    }

    @ApiOperation("班主任详情查询")
    @PostMapping("/detailToLeader")
    public AjaxResult detailToApp(@RequestBody QueryInspectionDTO queryInspectionDTO) {
        if (StringUtils.isEmpty(queryInspectionDTO.getOrgCode())) {
            throw new ServiceException("orgCode不能为空");
        }
        InspectionDetailAPPVO scoreDetailVO = inspectionService.detailToApp(queryInspectionDTO);
        return success(scoreDetailVO);
    }


    @ApiOperation("我的记录")
    @PostMapping("/getRecordByUseCode")
    public TableDataInfo getRecordByUseCode(@RequestBody QueryInspectionDTO queryInspectionDTO) {
        if (StringUtils.isEmpty(queryInspectionDTO.getOrgCode())) {
            throw new ServiceException("orgCode不能为空");
        }
        List<InspectionDetailVO> resulList = inspectionService.getRecordByUseCode(queryInspectionDTO);
        return getDataTable(resulList);
    }

    @ApiOperation("我的记录")
    @GetMapping("/area/app")
    @Anonymous
    public AjaxResult statisticsAreaApp() {
        return AjaxResult.success(inspectionService.statisticsAreaApp());
    }
}
